package com.kalua.list.dbaccess.retailer;

import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
import static com.datastax.driver.core.querybuilder.QueryBuilder.in;
import static com.datastax.driver.core.querybuilder.QueryBuilder.set;
import static com.kalua.list.dbaccess.retailer.RetailerRowMapper.LOGO_URI_COL_NAME;
import static com.kalua.list.dbaccess.retailer.RetailerRowMapper.RETAILER_COL_FAMILY;
import static com.kalua.list.dbaccess.retailer.RetailerRowMapper.RETAILER_ID_COL_NAME;
import static com.kalua.list.dbaccess.retailer.RetailerRowMapper.TRADING_NAME_COL_NAME;
import static java.util.stream.Collectors.toList;

import java.util.Collection;

import org.springframework.cassandra.core.RowMapper;
import org.springframework.data.cassandra.core.CassandraTemplate;

import com.datastax.driver.core.Statement;
import com.datastax.driver.core.querybuilder.Insert;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.kalua.list.dbaccess.AbstractCRUDDAO;
import com.kalua.list.domainmodel.TextIdentifier;
import com.kalua.list.domainmodel.retailer.Retailer;

public class RetailerDAOImpl 	extends AbstractCRUDDAO<Retailer, TextIdentifier> 
												implements RetailerDAO {

	public RetailerDAOImpl(CassandraTemplate template) 
	{ super(template); }	
	
	@Override
	protected Statement buildInsertStatement(Retailer entity) {
		Insert insert =  
				QueryBuilder	.insertInto(RETAILER_COL_FAMILY)
										.value(RETAILER_ID_COL_NAME, entity.getId().getValue())
			        					.value(TRADING_NAME_COL_NAME, entity.getTradingName())
			        					.value(LOGO_URI_COL_NAME, entity.getLogoURI())
										.ifNotExists();
		return insert;
	}

	@Override
	protected Statement buildMultiRowSelect(Object[] keys) {
		Statement statement = QueryBuilder.select().all()
				.from(RETAILER_COL_FAMILY)
				.where(in(RETAILER_ID_COL_NAME, keys));
		return statement;
	}

	@Override
	protected Statement buildReadStatement(TextIdentifier id) {
		Statement statement = QueryBuilder.select().all()
				.from(RETAILER_COL_FAMILY)
				.where(eq(RETAILER_ID_COL_NAME, id.getValue()));
		return statement;

	}

	@Override
	protected Statement buildReadAllStatement() {
		Statement statement = 
				QueryBuilder	.select().all()
										.from(RETAILER_COL_FAMILY);
		return statement;
	}

	@Override
	protected Statement buildUpdateStatement(Retailer entity) {
		Statement update =  
				QueryBuilder	.update(RETAILER_COL_FAMILY)
			        					.with(set(TRADING_NAME_COL_NAME, entity.getTradingName()))
			        					.and(set(LOGO_URI_COL_NAME, entity.getLogoURI()))
			        					.where(eq(RETAILER_ID_COL_NAME, entity.getId().getValue()));
		return update;
	}

	@Override
	protected Statement buildDeleteStatement(TextIdentifier id) {
		Statement statement = 
				QueryBuilder.delete()
		        		.from(RETAILER_COL_FAMILY)
		        		.where(eq(RETAILER_ID_COL_NAME, id.getValue()));
		return statement;
	}

	@Override
	protected RowMapper<Retailer> getRowMapper() 
	{ return new RetailerRowMapper(); }

	@Override
	protected Object[] toObjectArray(Collection<TextIdentifier> keys) 
	{ return keys.stream().map(k -> k.getValue()).collect(toList()).toArray(); }
}
